package com.miracle9.game.service;

import com.miracle9.game.dao.PayApplyServiceDao;
import com.miracle9.game.dao.PromoterServiceDao;
import com.miracle9.game.dao.UserServiceDao;
import com.miracle9.game.entity.PayApply;
import com.miracle9.game.entity.Promoter;
import com.miracle9.game.entity.User;
import org.apache.mina.core.session.IoSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("payApplyService")
public class PayApplyService {

    public static Object expiryLock = new Object();
    @Autowired
    private UserServiceDao userServiceDao;
    @Autowired
    private PromoterServiceDao promoterServiceDao;
    @Autowired
    private PayApplyServiceDao payApplyServiceDao;

    public List<PayApply> getAllPayApply() {
        return this.payApplyServiceDao.getAllPayApply();
    }

    public boolean updateStatus(int id, PayApply.PayApplyState state) {
        return this.payApplyServiceDao.updateStatus(id, state);
    }

    public void updateNotice(int id, boolean notice) {
        this.payApplyServiceDao.updateNotice(id, notice);
    }

    public Map<String, Object> refuseUserPayApply(int userId, IoSession session) {
        Map<String, Object> result = new HashMap<>();
        User user = (User) userServiceDao.getById(userId);
        if (user == null || user.getPayMoney() <= 0) {
            result.put("success", Boolean.FALSE);
            result.put("message", "无充值申请");
            return result;
        }
        PayApply payApply = payApplyServiceDao.getByuserIdAndUserType_0(userId, PayApply.PayApplyType.PAY.ordinal());
        if (payApply != null) {
            payApplyServiceDao.updateStatus(payApply.getId(), PayApply.PayApplyState.WAIVE);
        }
        userServiceDao.updatePayMoney(userId, 0);
        result.put("success", Boolean.TRUE);
        result.put("message", "");
        return result;
    }

    public Map<String, Object> refuseUserExpiryApply(int userId, IoSession session) {
        Map<String, Object> result = new HashMap<>();
        User user = (User) userServiceDao.getById(userId);
        if (user == null || user.getExpiryNum() <= 0) {
            result.put("success", Boolean.FALSE);
            result.put("message", "无兑奖申请");
            return result;
        }
        PayApply payApply = payApplyServiceDao.getByuserIdAndUserType_0(userId, PayApply.PayApplyType.EXPIRY.ordinal());
        if (payApply != null) {
            payApplyServiceDao.updateStatus(payApply.getId(), PayApply.PayApplyState.WAIVE);
        }
        userServiceDao.updateExpiryNum(userId, 0);
        result.put("success", Boolean.TRUE);
        result.put("message", "");
        return result;
    }

    public Map<String, Object> refusePromoterPayApply(int userId, IoSession session) {
        Map<String, Object> result = new HashMap<>();
        Promoter promoter = (Promoter) promoterServiceDao.getById(userId);
        if (promoter == null || promoter.getPayMoney() <= 0) {
            result.put("success", Boolean.FALSE);
            result.put("message", "无充值申请");
            return result;
        }
        PayApply payApply = payApplyServiceDao.getByuserIdAndUserType_1(userId, PayApply.PayApplyType.PAY.ordinal());
        if (payApply != null) {
            payApplyServiceDao.updateStatus(payApply.getId(), PayApply.PayApplyState.WAIVE);
        }
        promoterServiceDao.updatePayMoney(userId, 0);
        result.put("success", Boolean.TRUE);
        result.put("message", "");
        return result;
    }

    public Map<String, Object> refusePromoterExpiryApply(int userId, IoSession session) {
        Map<String, Object> result = new HashMap<>();
        Promoter promoter = (Promoter) promoterServiceDao.getById(userId);
        if (promoter == null || promoter.getExpiryNum() <= 0) {
            result.put("success", Boolean.FALSE);
            result.put("message", "无兑奖申请");
            return result;
        }
        PayApply payApply = payApplyServiceDao.getByuserIdAndUserType_1(userId, PayApply.PayApplyType.EXPIRY.ordinal());
        if (payApply != null) {
            payApplyServiceDao.updateStatus(payApply.getId(), PayApply.PayApplyState.WAIVE);
        }
        promoterServiceDao.updateExpiryNum(userId, 0);
        result.put("success", Boolean.TRUE);
        result.put("message", "");
        return result;
    }

}
